﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;

using EntitySpaces.Interfaces;

public partial class Admin_FinQry_FinQry001 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["Dsd"] != null)
            {
                txtDocumentStartDate.Value = Session["Dsd"].ToString();
            }
            if (Session["Ded"] != null)
            {
                txtDocumentEndDate.Value = Session["Ded"].ToString();
            }
            if (Session["Ssd"] != null)
            {
                txtStoreStartDate.Value = Session["Ssd"].ToString();
            }
            if (Session["Sed"] != null)
            {
                txtStoreEndDate.Value = Session["Sed"].ToString();
            }

            if (Session["txtBoxNo"] != null)
            { txtBoxNo.Text = Session["txtBoxNo"].ToString(); }
            if (Session["txtSupplierBoxNo"] != null)
            { txtSupplierBoxNo.Text = Session["txtSupplierBoxNo"].ToString(); }
            if (Session["drpDocType"] != null)
            {
                drpDocType.SelectedValue = Session["drpDocType"].ToString();
            }
            if (Session["txtKey"] != null)
            { txtKey.Text = Session["txtKey"].ToString(); }
            if (Session["drpCompany"] != null)
            {
                drpCompany.SelectedValue = Session["drpCompany"].ToString();
            }
            if (Session["drpFunctionDept"] != null)
            {
                drpFunctionDept.SelectedValue = Session["drpFunctionDept"].ToString();
            }
            if (Session["txtUserName"] != null)
            { txtUserName.Text = Session["txtUserName"].ToString(); }
            if (Session["txtDocumentsType"] != null)
            { txtDocumentsType.Text = Session["txtDocumentsType"].ToString(); }
            if (Session["txtReferenceTransact"] != null)
            { txtReferenceTransact.Text = Session["txtReferenceTransact"].ToString(); }
            if (Session["txtDocumentSystem"] != null)
            { txtDocumentSystem.Text = Session["txtDocumentSystem"].ToString(); }
            if (Session["txtLogicalSystem"] != null)
            { txtLogicalSystem.Text = Session["txtLogicalSystem"].ToString(); }

            if (Session["txtKeyStart"] != null)
            { txtKeyStart.Text = Session["txtKeyStart"].ToString(); }
            if (Session["txtKeyEnd"] != null)
            { txtKeyEnd.Text = Session["txtKeyEnd"].ToString(); }
            if (Session["txtKeyMulti"] != null)
            { txtKeyMulti.Text = Session["txtKeyMulti"].ToString(); }
            if (Session["drpKeyType"] != null)
            {
                drpKeyType.SelectedValue = Session["drpKeyType"].ToString();
            }
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["txtBoxNo"] = txtBoxNo.Text.Trim();
        Session["txtSupplierBoxNo"] = txtSupplierBoxNo.Text.Trim();
        Session["drpDocType"] = drpDocType.SelectedValue;
        Session["txtKey"] = txtKey.Text.Trim();
        Session["drpCompany"] = drpCompany.SelectedValue;
        Session["drpFunctionDept"] = drpFunctionDept.SelectedValue;
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["Dsd"] = txtDocumentStartDate.Value;
        Session["Ded"] = txtDocumentEndDate.Value;
        Session["Ssd"] = txtStoreStartDate.Value;
        Session["Sed"] = txtStoreEndDate.Value;
        Session["txtUserName"] = txtUserName.Text.Trim();
        Session["txtDocumentsType"] = txtDocumentsType.Text.Trim();
        Session["txtReferenceTransact"] = txtReferenceTransact.Text.Trim();
        Session["txtDocumentSystem"] = txtDocumentSystem.Text.Trim();
        Session["txtLogicalSystem"] = txtLogicalSystem.Text.Trim();

        Session["txtKeyStart"] = txtKeyStart.Text.Trim();
        Session["txtKeyEnd"] = txtKeyEnd.Text.Trim();
        Session["drpKeyType"] = drpKeyType.SelectedValue;
        Session["txtKeyMulti"] = txtKeyMulti.Text.Trim();
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");
        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFCompanyDicQuery q7 = new BFCompanyDicQuery("q7");
        VReceiptExtInfoQuery q8 = new VReceiptExtInfoQuery("q8");
        TSCodeTableQuery q9 = new TSCodeTableQuery("q9");



        q1.LeftJoin(q9).On(q1.VoucherDescription == q9.Code);
        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.VoucherDescription == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.Division == q6.Code);
        q1.LeftJoin(q7).On(q1.CompanyCode == q7.CompanyCode);
        q1.LeftJoin(q8).On(q1.ReceiptID == q8.ReceiptID);


        q1.SelectAll().Select(q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("VoucherDescriptionName"), q5.Name.As("StorageLocationStr"), q6.Value1.As("DivisionStr"), q7.CompanyName, q7.CompanyCode, q8.SupplierBoxNo, q8.BoxNo, q9.Value1.As("DocDesc"));

        ArrayList args = new ArrayList();
        ArrayList argsOr = new ArrayList();
        args.Add(q1.IsSAP == true);

        if (!string.IsNullOrEmpty(drpDocType.SelectedValue))
        {
            args.Add(q1.VoucherDescription == drpDocType.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txtKey.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.Reference.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.ReferenceKey.Like("%" + txtKey.Text.Trim() + "%"));
            argsOr.Add(q1.DocumentHeaderText.Like("%" + txtKey.Text.Trim() + "%"));
            args.Add(q1.Or(argsOr.ToArray()));
        }

        if (!string.IsNullOrEmpty(txtKeyStart.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo >= txtKeyStart.Text.Trim());
            argsOr.Add(q1.Reference >= txtKeyStart.Text.Trim());
            argsOr.Add(q1.ReferenceKey >= txtKeyStart.Text.Trim());
            argsOr.Add(q1.DocumentHeaderText >= txtKeyStart.Text.Trim());
            args.Add(q1.Or(argsOr.ToArray()));
        }

        if (!string.IsNullOrEmpty(txtKeyEnd.Text.Trim()))
        {
            argsOr.Add(q1.DocumentsNo <= txtKeyEnd.Text.Trim());
            argsOr.Add(q1.Reference <= txtKeyEnd.Text.Trim());
            argsOr.Add(q1.ReferenceKey <= txtKeyEnd.Text.Trim());
            argsOr.Add(q1.DocumentHeaderText <= txtKeyEnd.Text.Trim());
            args.Add(q1.Or(argsOr.ToArray()));
        }

        if (!string.IsNullOrEmpty(txtKeyMulti.Text.Trim()))
        {
            
            switch (drpKeyType.SelectedValue)
            {
                case "1":
                    string[] keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r','\n' });
                    for (int i = 0; i < keys.Length;i++ )
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.DocumentsNo == keys[i]); }
                    }
                    break;
                case "2":
                    keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length;i++ )
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.Reference == keys[i]); }
                    }
                    break;
                case "3":
                    keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length;i++ )
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.ReferenceKey == keys[i]); }
                    }
                    break;
                case "4":
                    keys = txtKeyMulti.Text.Trim().Split(new char[] { '\r', '\n' });
                    for (int i = 0; i < keys.Length;i++ )
                    {
                        if (!string.IsNullOrEmpty(keys[i]))
                        { argsOr.Add(q1.DocumentHeaderText == keys[i]); }
                    }
                    break;
            }
            args.Add(q1.Or(argsOr.ToArray()));
        }


        if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
        {
            args.Add(q1.CompanyName == drpCompany.SelectedValue.Split('/')[0]);
            args.Add(q1.CompanyCode == drpCompany.SelectedValue.Split('/')[1]);
        }
        if (!string.IsNullOrEmpty(drpFunctionDept.SelectedValue))
        {
            args.Add(q1.FunctionDept == drpFunctionDept.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txtDocumentStartDate.Value))
        {
            args.Add(q1.VoucherDate >= txtDocumentStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtDocumentEndDate.Value))
        {
            args.Add(q1.VoucherDate <= DateTime.Parse(txtDocumentEndDate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtStoreStartDate.Value))
        {
            args.Add(q1.FirstStorageDate >= txtStoreStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtStoreEndDate.Value))
        {
            args.Add(q1.FirstStorageDate <= DateTime.Parse(txtStoreEndDate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtStartDate.Value))
        {
            args.Add(q1.CreatedDttm >= txtStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtEndate.Value))
        {
            args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
        {
            args.Add(q1.UserName.Like("%" + txtUserName.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentsType.Text.Trim()))
        {
            args.Add(q1.DocumentsType.Like("%" + txtDocumentsType.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtReferenceTransact.Text.Trim()))
        {
            args.Add(q1.ReferenceTransact.Like("%" + txtReferenceTransact.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtDocumentSystem.Text.Trim()))
        {
            args.Add(q1.DocumentSystem.Like("%" + txtDocumentSystem.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtLogicalSystem.Text.Trim()))
        {
            args.Add(q1.LogicalSystem.Like("%" + txtLogicalSystem.Text.Trim() + "%"));
        }

        if (args.Count > 1)
        {
            q1.Where(args.ToArray()).OrderBy(q1.DocumentsNo.Ascending);
            q1.es.Top = 1000;
            DataTable tableSorce = q1.LoadDataTable();
            pageGrid.DataSource = tableSorce;
            pageGrid.CurrentPage = e.NewPageIndex;
            pageGrid.DataBind();
        }
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("drpDocType");
        Session.Remove("txtKey");
        Session.Remove("drpCompany");
        Session.Remove("drpFunctionDept");
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("Dsd");
        Session.Remove("Ded");
        Session.Remove("Ssd");
        Session.Remove("Sed");
        Session.Remove("txtUserName");
        Session.Remove("txtDocumentsType");
        Session.Remove("txtReferenceTransact");
        Session.Remove("txtDocumentSystem");
        Session.Remove("txtLogicalSystem");
        Session.Remove("txtBoxNo");
        Session.Remove("txtSupplierBoxNo");
    }


}